/**
 * \class CServiceRepository
 * \brief This is a service container which provide a multi-threading server
 * \version 1.0.2
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2008/2/12
 */
//-----------------------------------------------------------------
#ifndef CServiceRepositoryH
#define CServiceRepositoryH
//-----------------------------------------------------------------
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include <pthread.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "coord_conf.h"

#include "file/CFile.h"
#include "space/CSpace.h"
#include "property/CProperty.h"

#include "CServiceContainer.h"

#include "CMIMEServerCallback.h"

#define BACK_LOG 100
#define MAX_THREAD 50
#define MAX_QUEUE 1000

using namespace std;

class CServiceRepository : public CServiceContainer
{
private:
	string conf;

	string ip;
	int port;

	// status
	float cpu;
	float load;

	// queue ----
	static int head;
	static int tail;
	static SOAP_SOCKET sock_queue[MAX_QUEUE];
	static pthread_mutex_t queue_cs;
	static pthread_cond_t queue_cv;

	static int enqueue(SOAP_SOCKET);
	static SOAP_SOCKET dequeue();
	// ----------

	int analyze_role_and_get_port();

	static void *process_thread(void *pf);	

protected:
	virtual void init();

	virtual int activate(CService *service);
	virtual int inactivate(CService *service);

public:
	static CProperty *config;

	static CSpace *space;
	static CSpace *repository;

        CServiceRepository();
        CServiceRepository(int port);
        CServiceRepository(char *conf);
        virtual ~CServiceRepository();

	char *getIP();
	int getPort();

	float getCPU();
	float getLoad();

	void runCGI();
	void run();
};
#endif
